(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.acctType.updateContinueController', updateContinueController);
	updateContinueController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'acctTypeService' ];
	function updateContinueController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, acctTypeService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		var log = $scope.loggerFactory.getLogger("acctTypeCreateContinue");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.acctType = {};
		
		ctrl.init = function() {
			authService.updateContinue(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {	
					ctrl.acctType = response.data.model;
					ctrl.acctType.taskId = response.data.task.taskId;
				}
			});
		};

		ctrl.updateContinueSave = function() {
			log.debug('prop in acctType:');
			for (var name in ctrl.acctType) {
				log.debug(name + '=' + ctrl.acctType[name]);
			}
			return authService.updateContinueSave(ctrl.acctType).then(saveSuccessful, saveFailed);
		};

		ctrl.updateContinueSubmit = function() {
			return authService.updateContinueSubmit(ctrl.acctType).then(submitSuccessful, submitFailed);
		};

		ctrl.updateContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.acctType.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateContinueCancel(ctrl.acctType).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.accttype.list',
					'state' : 'cda.acctType.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.acctType = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.accttype.list',
					'state' : 'cda.acctType.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);